#include "NozzleInspectDockWidget.h"
#include <QtWidgets/QLabel>
#include <QtWidgets/QMessageBox>

NozzleInspectDockWidget::NozzleInspectDockWidget(QWidget *parent, Qt::WindowFlags flags)
    :QDockWidget(parent, flags)
{
    initContent();
    initConnection();
}

void NozzleInspectDockWidget::gotoRowCol()
{
    int col = spinBoxGotoCol->value();
    int row = spinBoxGotoRow->value();
    double x_mm, y_mm;
    m_holder.getPosition(row - 1, col - 1, x_mm, y_mm);
    emit gotoPositon(x_mm, y_mm, 10.0);

}

void NozzleInspectDockWidget::setNozzleZeroPos()
{
    QMessageBox msgBox;
    msgBox.setText(QStringLiteral("是否设置当前位置为 Nozzle 的原点？\n * 选择 Restore Defaults 则恢复出厂时默认的 Nozzle 原点位置。"));
    msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No|QMessageBox::RestoreDefaults);
    msgBox.setDefaultButton(QMessageBox::No);

    int ret = msgBox.exec();

    switch(ret)
    {
    case QMessageBox::Yes:
        m_stageView->setNozzleZeroPos();
        m_holder.setNozzleZeroPos(m_currentX, m_currentY);
        break;
    case QMessageBox::RestoreDefaults:
        m_stageView->resetNozzleZeroPos();
        m_holder.resetNozzleZeroPos();
        break;
    default:
        break;
    }
}

void NozzleInspectDockWidget::loadSettings(QSettings &settings)
{
    m_stageView->loadSettings(settings);
    m_holder.loadSettings(settings);
}

void NozzleInspectDockWidget::setPosition(double x_mm, double y_mm)
{
    m_currentX = x_mm;
    m_currentY = y_mm;
    m_stageView->setPosition(x_mm, y_mm);
}

void NozzleInspectDockWidget::initContent()
{
    dockWidget = new QWidget(this);
    verticalLayout = new QVBoxLayout;
//    m_verticalLayout->setSpacing(6);
//    m_verticalLayout->setContentsMargins(11, 11, 11, 11);
    horizontalLayoutGoto = new QHBoxLayout;
    pushButtonGotoRowCol = new QPushButton(this);
    pushButtonGotoRowCol->setText("GOTO");
    horizontalLayoutGoto->addWidget(pushButtonGotoRowCol);
    spinBoxGotoRow = new QSpinBox(this);
    spinBoxGotoRow->setMinimum(1);
    spinBoxGotoRow->setMaximum(4);
    horizontalLayoutGoto->addWidget(spinBoxGotoRow);

    spinBoxGotoCol = new QSpinBox(this);
    spinBoxGotoCol->setMaximum(8);
    spinBoxGotoCol->setMinimum(1);
    horizontalLayoutGoto->addWidget(spinBoxGotoCol);
    verticalLayout->addLayout(horizontalLayoutGoto);
    horizontalLayout = new QHBoxLayout;
//    m_horizontalLayout->setSpacing(6);

    QLabel* label = new QLabel(dockWidget);
    label->setText(QString::fromUtf8(u8"喷嘴直径"));
    horizontalLayout->addWidget(label);

    comboBoxNozzle = new QComboBox(dockWidget);
    comboBoxNozzle->insertItem(0, "0.25mm");
    comboBoxNozzle->insertItem(1, "0.30mm");
    comboBoxNozzle->insertItem(2, "0.35mm");
    comboBoxNozzle->insertItem(3, "0.40mm");
    comboBoxNozzle->insertItem(4, "0.45mm");
    comboBoxNozzle->insertItem(5, "0.50mm");
    horizontalLayout->addWidget(comboBoxNozzle);

    pushButtonNozzleMeasure = new QPushButton(dockWidget);
    pushButtonNozzleMeasure->setText(QString::fromUtf8(u8"自动测量"));
    horizontalLayout->addWidget(pushButtonNozzleMeasure);

    verticalLayout->addLayout(horizontalLayout);

    horizontalLayout_2 = new QHBoxLayout;
    pushButtonNozzleZero = new QPushButton(dockWidget);
    pushButtonNozzleZero->setText(QString::fromUtf8(u8"喷嘴零位"));
    horizontalLayout_2->addWidget(pushButtonNozzleZero);

    pushButtonBatchMeasure = new QPushButton(dockWidget);
    pushButtonBatchMeasure->setText(QString::fromUtf8(u8"批量测量"));
    horizontalLayout_2->addWidget(pushButtonBatchMeasure);

    pushButtonShowFileNameDialog = new QPushButton(dockWidget);
    pushButtonShowFileNameDialog->setText(QString::fromUtf8(u8"设置文件名"));
    horizontalLayout_2->addWidget(pushButtonShowFileNameDialog);

    verticalLayout->addLayout(horizontalLayout_2);

    horizontalLayout_3 = new QHBoxLayout;
    pushButtonSaveWithMark = new QPushButton(dockWidget);
    pushButtonSaveWithMark->setText(QString::fromUtf8(u8"保存图像（带标尺）"));
    horizontalLayout_3->addWidget(pushButtonSaveWithMark);

    pushButtonSaveWithoutMark = new QPushButton(dockWidget);
    pushButtonSaveWithoutMark->setText(QString::fromUtf8(u8"保存图像（不带标尺）"));
    horizontalLayout_3->addWidget(pushButtonSaveWithoutMark);

    verticalLayout->addLayout(horizontalLayout_3);

    m_stageView = new StageView(dockWidget);
    verticalLayout->addWidget(m_stageView);
    verticalLayout->addStretch(10);
    dockWidget->setLayout(verticalLayout);
    setWidget(dockWidget);

    setWindowTitle(QString::fromUtf8(u8"Nozzle 检测"));
}

void NozzleInspectDockWidget::initConnection()
{
    connect(pushButtonSaveWithMark, SIGNAL(clicked(bool)), this, SIGNAL(saveImageWithMark()));
    connect(pushButtonSaveWithoutMark, SIGNAL(clicked(bool)), this, SIGNAL(saveImage()));
    connect(pushButtonShowFileNameDialog, SIGNAL(clicked(bool)), this, SIGNAL(showFileNameDialog()));
    connect(pushButtonBatchMeasure, SIGNAL(clicked(bool)),this, SIGNAL(batchMeasure()));

    connect(pushButtonNozzleZero, SIGNAL(clicked(bool)), this, SLOT(setNozzleZeroPos()));
    connect(pushButtonGotoRowCol, SIGNAL(clicked(bool)), this, SLOT(gotoRowCol()));

    connect(pushButtonNozzleMeasure, SIGNAL(clicked(bool)), this, SIGNAL(innerCircleMeasure()));
}

NozzleInspectDockWidget::~NozzleInspectDockWidget()
{

}
